package TUILayer;

import ControlLayer.LeaseItemCtr;
import ModelLayer.RealException;
import java.util.ArrayList;

public class LeaseItemUI {

	private LeaseItemCtr leaseItemCtr;
	private InputParser inputParser;
	private LoginInfo loginInfo;

	public LeaseItemUI(InputParser inputParser, LoginInfo loginInfo) {
		leaseItemCtr = new LeaseItemCtr();
		this.inputParser = inputParser;
		this.loginInfo = loginInfo;
	}

	public void start() throws RealException {
		int choice = 0;
		boolean exit = false;
		switch (loginInfo.getLoggedEmployeePositionId()) {
		case 1: {
			do {
				try {
					choice = inputParser
							.getInt("\f *** LeaseProductMenu ***\n\n1. Show lease item information\n2. Create lease item\n3. Update lease item\n4. Delete lease item\n5. Create lease item unit\n6. Delete lease item unit\n7. List of all lease items and it's units\n8. Find lease item and it's units\n9. Return\n\nMake your choice: ");
					switch (choice) {
					case 1:
						showLeaseItemInfo();
						break;
					case 2:
						createLeaseItem();
						break;
					case 3:
						updateLeaseItem();
						break;
					case 4:
						deleteLeaseItem();
						break;
					case 5:
						createLeaseItemUnit();
						break;
					case 6:
						deleteLeaseItemUnit();
						break;
					case 7:
						listAllLeaseItems();
						break;
					case 8:
						findLeaseItem();
						break;
					case 9:
						exit = true;
						break;
					}
				} catch (RealException e) {
					System.out.println("\nSomething went wrong\n"
							+ e.getError() + " Please try again");
					inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		case 2: {
			do {
				try {
					choice = inputParser
							.getInt("\f *** LeaseProductMenu ***\n\n1. Show lease item information\n2. Create lease item\n3. Update lease item\n4. Delete lease item\n5. Create lease item unit\n6. Delete lease item unit\n7. List of all lease items and it's units\n8. Find lease item and it's units\n9. Return\n\nMake your choice: ");
					switch (choice) {
					case 1:
						showLeaseItemInfo();
						break;
					case 2:
						createLeaseItem();
						break;
					case 3:
						updateLeaseItem();
						break;
					case 4:
						deleteLeaseItem();
						break;
					case 5:
						createLeaseItemUnit();
						break;
					case 6:
						deleteLeaseItemUnit();
						break;
					case 7:
						listAllLeaseItems();
						break;
					case 8:
						findLeaseItem();
						break;
					case 9:
						exit = true;
						break;
					}
				} catch (RealException e) {
					System.out.println("\nSomething went wrong\n"
							+ e.getError() + " Please try again");
					inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		case 3: {
			do {
				try {
					choice = inputParser
							.getInt("\f *** LeaseProductMenu ***\n\n1. Show lease item information\n2. Update lease item location\n3. List of all lease items and it's units\n4. Find lease item and it's units\n5. Return\n\nMake your choice: ");
					switch (choice) {
					case 1:
						showLeaseItemInfo();
						break;
					case 2: {
						int id = inputParser
								.getInt("\f ***UpdateLeaseItemMenu ***\n\nEnter the lease item ID: ");
						boolean found = true;
						try {
							leaseItemCtr.checkLeaseItem(id);
						} catch (RealException e) {
							found = false;
							System.out
									.println("\f ***UpdateLeaseItemMenu ***\n\n"
											+ e.getError());
							inputParser.printReturn();
						}
						if (found) {
							String value = String.valueOf(inputParser
									.getInt("\f ***UpdateLeaseItemMenu ***\n\nEnter new location number: "));
							leaseItemCtr.updateLeaseItem(id, 2, value);
							inputParser.printReturn();
						}
						break;
					}
					case 3:
						listAllLeaseItems();
						break;
					case 4:
						findLeaseItem();
						break;
					case 5:
						exit = true;
						break;
					}
				} catch (RealException e) {
					System.out.println("\nSomething went wrong\n"
							+ e.getError() + " Please try again");
					inputParser.printReturn();
				}
			} while (!exit);
			break;
		}
		}
	}

	public void showLeaseItemInfo() throws RealException {
		int id = inputParser
						.getInt("\f ***ShowLeaseItemMenu ***\n\n Please enter lease item ID: ");
		System.out.println("\f ***ShowLeaseItemMenu ***\n");
		try {
			ArrayList<String> infos = leaseItemCtr.getLeaseItemInfo(id);
			for (String info : infos)
				System.out.println(info);
		} catch (RealException e) {
			System.out.println(e.getError());
		}
		inputParser.printReturn();
	}

	public void createLeaseItem() throws RealException {
		String name = inputParser
				.getString("\f ***CreateLeaseItemMenu ***\n\nEnter the lease item name: ");
		double price = inputParser
				.getDouble("\f ***CreateLeaseItemMenu ***\n\nEnter the lease price: ");
		int location = inputParser
				.getInt("\f ***CreateLeaseItemMenu ***\n\nEnter the lease item location number: ");
		leaseItemCtr.createLeaseItem(name, location, price);
		inputParser.printReturn();
	}

	public void updateLeaseItem() throws RealException {
		int id = inputParser
				.getInt("\f ***UpdateLeaseItemMenu ***\n\nEnter the lease item ID: ");
		boolean found = true;
		try {
			leaseItemCtr.checkLeaseItem(id);
		} catch (RealException e) {
			found = false;
			System.out.println("\f ***UpdateLeaseItemMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
		if (found) {
			int choice;
			String value = "";
			boolean exit = false;
			do {
				choice = inputParser
						.getInt("\f ***UpdateLeaseItemMenu ***\n\nChoose a parameter to change:\n(1) for name.\n(2) for location.\n(3) for lease price.\n(4) Return.");
				switch (choice) {
				case 1: {
					value = inputParser
							.getString("\f ***UpdateLeaseItemMenu ***\n\nEnter new name: ");
					break;
				}
				case 2: {
					value = String.valueOf(inputParser
							.getInt("\f ***UpdateLeaseItemMenu ***\n\nEnter new location number: "));
					break;
				}
				case 3: {
					value = String.valueOf(inputParser
							.getDouble("\f ***UpdateLeaseItemMenu ***\n\nEnter new lease price: "));
					break;
				}
				case 4: {
					exit = true;
					break;
				}
				}
				if (choice >= 1 && choice <= 3) {
					leaseItemCtr.updateLeaseItem(id, choice, value);
					System.out
							.println("\f ***UpdateLeaseItemMenu ***\n\nLease Item updated.");
					inputParser.printReturn();
				}
			} while (!exit);
		}
	}

	public void deleteLeaseItem() throws RealException {
		int id = inputParser
				.getInt("\f ***DeleteLeaseItemMenu ***\n\nEnter ID of lease item you want to delete: ");
		leaseItemCtr.deleteLeaseItem(id);
		inputParser.printReturn();
	}

	public void createLeaseItemUnit() throws RealException {
		int itemID = inputParser
				.getInt("\f ***CreateLeaseItemUnitMenu ***\n\nEnter lease item ID, to add new unit: ");
		leaseItemCtr.createLeaseItemUnit(itemID);
		System.out
				.println("\f ***CreateLeaseItemUnitMenu ***\n\n Lease item unit added.");
		inputParser.printReturn();
	}

	public void deleteLeaseItemUnit() throws RealException {
		int itemID = inputParser
				.getInt("\f ***DeleteLeaseItemUnitStatusMenu ***\n\nEnter lease item ID, to delete one of it's units: ");
		boolean found = true;
		try {
			leaseItemCtr.checkLeaseItem(itemID);
		} catch (RealException e) {
			found = false;
			System.out.println("\f ***UpdateLeaseItemMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
		if (found) {
			int unitSerialNo = inputParser
					.getInt("\f ***DeleteLeaseItemUnitStatusMenu ***\n\nEnter lease item unit serial number, to be deleted: ");
			leaseItemCtr.deleteLeaseUnit(itemID, unitSerialNo);
			inputParser.printReturn();
		}
	}

	public void listAllLeaseItems() throws RealException {
		ArrayList<String> leaseItemsInfo = leaseItemCtr.getAllLeaseItemInfo();
		System.out.println("\f ***ListAllLeaseItems ***\n");
		for (String leaseItemInfo : leaseItemsInfo) {
			System.out.println(leaseItemInfo);
		}
		inputParser.printReturn();
	}

	public void findLeaseItem() throws RealException {
		String searchPattern = inputParser
				.getString("\f ***FindLeaseItemMenu ***\n\nPlease enter start of the lease item name you are searching for: ");
		ArrayList<String> matchingLeaseItems = leaseItemCtr
				.findLeaseItem(searchPattern);
		System.out.println("\f ***MatchingLeaseItems ***\n");
		for (String leaseItemInfo : matchingLeaseItems) {
			System.out.println(leaseItemInfo);
		}
		inputParser.printReturn();

	}
}
